//
//  LGTRWZTaskHeadView.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/20.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import SDCycleScrollView

class LGTRWZTaskHeadView: UITableViewHeaderFooterView, SDCycleScrollViewDelegate{
    var xiajiaLabel:UILabel? //下架
    var sendLabel:UILabel? //派发中
    var stopLabel:UILabel? //暂停中
    var checkLabel:UILabel? //审核中
    var noPassLabel:UILabel? //未通过
    var completeLabel:UILabel? //已完成
    var rebackLabel:UILabel? //已撤回
    var outDateLabel:UILabel? //已过期
    var adsView:SDCycleScrollView? //广告界面
    //态[1-审核中,2-未通过,3-派发中,4-暂停中,5-已撤回,6-已过期,7-已完成 8-已下架] ,
    var keyArray:Array<String> = ["3", "4", "1", "2", "7", "5", "6", "8"]
    var labelInfo:Dictionary<String, UILabel> = [:]
    var labelArray:Array<UILabel> = Array.init()
    var bageInfo:Dictionary<String, UIImageView> = [:]
    var bageViewArray:Array<UIImageView> = Array.init()
    var clickBlock:ClickIndexBlock?
    var adsClickBlock:ClickInfoBlock?
    
    var _imageArray:Array<Dictionary<String, Any>>?
    var imageArray:Array<Dictionary<String, Any>>?{
        get{
            return _imageArray
        }set{
            _imageArray = newValue
            self.setImages()
        }
    }
    
    var _countArray:Array<Dictionary<String, Any>>?
    var countArray:Array<Dictionary<String, Any>>?{
        get{
            return _countArray
        }set{
            _countArray = newValue
            self.setData()
        }
    }
    
    //设置图片
    func setImages(){
        
        var imageArray:Array<String> = []
        for i in 0..<(self.imageArray?.count ?? 0){
            let dic = self.imageArray![i]
            let image = dic["img"] as? String ?? ""
            imageArray.append(image)
        }
        self.adsView?.imageURLStringsGroup = imageArray
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = UIColor.white
        
        let appNameView:UIImageView = LGTViewCreateUtil.share.createImageView(image: "app_name_logo")
        self.addSubview(appNameView)
        appNameView.snp.makeConstraints { (make) in
            make.width.equalTo(61)
            make.height.equalTo(22)
            make.left.equalTo(SizeFit(9))
            make.top.equalTo(36 + IPHONENAV_DIS())
        }
        
        self.adsView = SDCycleScrollView.init(frame: CGRect.init())
        self.adsView?.delegate = self
        self.adsView?.layer.masksToBounds = true
        self.adsView?.layer.cornerRadius = CGFloat(SizeFit(3))
        self.adsView?.backgroundColor = ViewBackColor
        self.addSubview(self.adsView!)
        self.adsView?.snp.makeConstraints({ (make) in
            make.left.equalTo(SizeFit(9))
            make.right.equalTo(-SizeFit(9))
            make.height.equalTo(SizeFit(120))
            make.top.equalTo(SizeFit(7) + NavBarH())
        })
        
        let sendLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: CGFloat(SizeFit(15)), text: "我的发布")
        self.addSubview(sendLabel)
        sendLabel.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(9))
            make.top.equalTo(self.adsView!.snp_bottomMargin).offset(SizeFit(22))
            make.height.equalTo(SizeFit(15))
        }
        
        let line:UIImageView = LGTViewCreateUtil.share.createLineView()
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(9))
            make.right.equalTo(-SizeFit(9))
            make.height.equalTo(1)
            make.top.equalTo(self.adsView!.snp_bottomMargin).offset(SizeFit(44))
        }
        
        self.bageViewArray = Array.init()
        self.xiajiaLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15))
        self.sendLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15))
        self.stopLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15))
        self.checkLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15))
        self.noPassLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15))
        self.completeLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15))
        self.rebackLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15))
        self.outDateLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15))
        
        self.labelArray = [self.sendLabel, self.stopLabel, self.checkLabel, self.noPassLabel, self.completeLabel, self.rebackLabel, self.outDateLabel, self.xiajiaLabel] as! Array<UILabel>
        let strArray = ["派发中", "暂停中", "审核中", "未通过", "已完成", "已撤回", "已过期", "已下架"]
        
        
//        let RWZOrderStateInfo:Dictionary<String, Any> = [
//            "1":[:],
//            "2":[:],
//            "3":[:],
//            "4":["title":"待审核", "color": ColorUtil(color: 0x00B55F),"time":"提交审核时间："],
//            "5":["title":"审核通过", "color": ColorUtil(color: 0x00B55F),"time":"审核通过时间："],
//            "6":["title":"审核未通过", "color": ColorUtil(color: 0x00B55F),"time":"审核时间："],
//            "7":["title":"审核未通过", "color": ColorUtil(color: 0x00B55F),"time":"审核时间："],
//            "8":["title":"申诉待处理", "color": ColorUtil(color: 0x3D88F8),"time":"提交申诉时间："],
//            "9":["title":"申诉通过", "color": ColorUtil(color: 0x3D88F8),"time":"处理申诉时间："],
//            "10":["title":"申诉未通过", "color": ColorUtil(color: 0x3D88F8),"time":"处理申诉时间："],
//            "11":["title":"申诉未通过", "color": ColorUtil(color: 0x3D88F8),"time":"处理申诉时间："],
//            "12":["title":"介入中", "color": ColorUtil(color: 0xFF551A),"time":"申请介入时间："],
//            "13":["title":"介入通过", "color": ColorUtil(color: 0xFF551A),"time":"介入处理时间："],
//            "14":["title":"介入未通过", "color": ColorUtil(color: 0xFF551A),"time":"介入处理时间："],
//            "15":["title":"审核通过", "color": ColorUtil(color: 0x00B55F),"time":"审核通过时间："],  //审核过期通过
//            "16":["title":"申诉通过", "color": ColorUtil(color: 0xFF551A),"time":"申诉通过时间："], //申诉过期通过
//        ]
        
        let count = 4
        let w = KScreenW / CGFloat(count)
        let h  = SizeFitFloat(76)
        for i in 0..<labelArray.count{
            let button:UIButton = UIButton.init()
            button.tag = i
            //button.backgroundColor = UIColor.orange
            self.addSubview(button)
            button.addTarget(self, action: #selector(clickAction), for: UIControl.Event.touchUpInside)
            button.snp.makeConstraints { (make) in
                make.left.equalTo(w * CGFloat(i % count))
                make.height.equalTo(h)
                make.width.equalTo(w)
                make.top.equalTo(line.snp_bottomMargin).offset(SizeFitFloat(2) + CGFloat(i / count) * h)
            }
            
            let label:UILabel = self.labelArray[i]
            button.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.centerX.equalTo(button)
                make.height.equalTo(SizeFit(15))
                make.top.equalTo(SizeFit(20))
                make.width.equalTo(w - 10)
            }
            
            let desc:String = strArray[i]
            let descLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(15), text: desc)
            button.addSubview(descLabel)
            descLabel.snp.makeConstraints { (make) in
                make.centerX.equalTo(button)
                make.height.equalTo(SizeFit(15))
                make.top.equalTo(SizeFit(45))
                make.width.equalTo(w - 10)
            }
            
            let bageView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(4))
            button.addSubview(bageView)
            bageView.backgroundColor = RedColor
            bageView.isHidden = true
            bageView.snp.makeConstraints { (make) in
                make.width.height.equalTo(SizeFit(8))
                make.right.equalTo(SizeFitFloat(-15))
                make.top.equalTo(SizeFitFloat(12))
            }
            
            self.bageViewArray.append(bageView)
            let key:String = self.keyArray[i]
            self.bageInfo[key] = bageView
            self.labelInfo[key] = label
        }
        
        let taskListLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: CGFloat(SizeFit(15)), text: "任务列表")
        self.addSubview(taskListLabel)
        taskListLabel.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(9))
            make.top.equalTo(SizeFit(315) + NavBarH())
            //make.top.equalTo(line.snp_bottomMargin).offset(SizeFit(315))
            make.height.equalTo(SizeFit(15))
        }
        
        let line2:UIImageView = LGTViewCreateUtil.share.createLineView()
        self.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(9))
            make.right.equalTo(-SizeFit(9))
            make.height.equalTo(1)
            make.bottom.equalTo(0)
            //make.top.equalTo(line.snp_bottomMargin).offset(SizeFit(169))
        }
        
//[0 全部 1-审核中,2-未通过,3-派发中,4-暂停中,5-已撤回,6-已过期,7-已完成]
//        self.labelArray = [self.allLabel, self.checkLabel, self.noPassLabel, self.sendLabel, self.stopLabel, self.rebackLabel, self.outDateLabel, self.completeLabel] as? Array<UILabel>
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func clickAction(sender:UIButton){
        if self.clickBlock != nil{
            self.clickBlock!(sender.tag)
        }
    }
    
    func setData(){
       //3-派发中,4-暂停中, 1-审核中, 2-未通过, 7-已完成, 5-已撤回, 6-已过期, 8-已下架],
        
        for i in 0..<self.labelArray.count{
            let label:UILabel = self.labelArray[i]
            let bageView:UIImageView = self.bageViewArray[i]
            label.text = "0"
            bageView.isHidden = true
        }
        
        let count = self.countArray?.count ?? 0
        for i in 0..<count{
            let d:Dictionary<String, Any> = self.countArray?[i] as! Dictionary<String, Any>
            let status:Int = d["status"] as? Int ?? 0
            let unread_num:Int = Int(d["unread_num"] as? String ?? "0") ?? 0
            let key:String = "\(status)"
            let label:UILabel? = self.labelInfo[key]
            let bageView:UIImageView? = self.bageInfo[key]
            bageView?.isHidden = unread_num > 0 ? false:true
            label?.text = d["num"] as? String ?? "0"
        }
    }
    
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
        if self.adsClickBlock != nil{
            self.adsClickBlock!(index, self.imageArray?[index] ?? [:])
        }
    }
}
